package org.xb;

/**
 * @Author
 * @Description 懒汉式
 * @Date created in 10:05 2024/10/22
 */
public class Main02 {
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                Person2 instance = Person2.getInstance();
                System.out.println(instance.hashCode());
            }).start();
        }
    }
}
class Person2 {
    private Person2() {
    }
    private static Person2 person;

    /**
     * 虽然是完成了懒汉式的单例 但是存在问题 锁的颗粒度太大了
     */
    public static synchronized Person2 getInstance() {
        if (person == null) {
            person = new Person2();
        }
        return person;
    }
}


